package cn.iocoder.yudao.module.crm.service.receipttitle;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.crm.controller.admin.contact.vo.CrmContactPageReqVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.crm.controller.admin.receipttitle.vo.*;
import cn.iocoder.yudao.module.crm.dal.dataobject.receipttitle.ReceiptTitleDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.crm.dal.mysql.receipttitle.ReceiptTitleMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.PageParam.PAGE_SIZE_NONE;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.crm.enums.ErrorCodeConstants.*;

/**
 * crm发票抬头 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ReceiptTitleServiceImpl implements ReceiptTitleService {

    @Resource
    private ReceiptTitleMapper receiptTitleMapper;

    @Override
    public Long createReceiptTitle(ReceiptTitleSaveReqVO createReqVO) {
        // 插入
        ReceiptTitleDO receiptTitle = BeanUtils.toBean(createReqVO, ReceiptTitleDO.class);
        receiptTitleMapper.insert(receiptTitle);

        // 返回
        return receiptTitle.getId();
    }

    @Override
    public void updateReceiptTitle(ReceiptTitleSaveReqVO updateReqVO) {
        // 校验存在
        validateReceiptTitleExists(updateReqVO.getId());
        // 更新
        ReceiptTitleDO updateObj = BeanUtils.toBean(updateReqVO, ReceiptTitleDO.class);
        receiptTitleMapper.updateById(updateObj);
    }

    @Override
    public void deleteReceiptTitle(Long id) {
        // 校验存在
        validateReceiptTitleExists(id);
        // 删除
        receiptTitleMapper.deleteById(id);
    }

    @Override
        public void deleteReceiptTitleListByIds(List<Long> ids) {
        // 删除
        receiptTitleMapper.deleteByIds(ids);
        }


    private void validateReceiptTitleExists(Long id) {
        if (receiptTitleMapper.selectById(id) == null) {
            throw exception(RECEIPT_TITLE_NOT_EXISTS);
        }
    }

    @Override
    public ReceiptTitleDO getReceiptTitle(Long id) {
        return receiptTitleMapper.selectById(id);
    }

    @Override
    public PageResult<ReceiptTitleDO> getReceiptTitlePage(ReceiptTitlePageReqVO pageReqVO) {
        return receiptTitleMapper.selectPage(pageReqVO);
    }


    @Override
    public List<ReceiptTitleDO> getReceiptTitleList(Long userId, Long customerId) {
        ReceiptTitlePageReqVO  reqVO = new ReceiptTitlePageReqVO();
        reqVO.setOwnerUserId(userId);
        reqVO.setCustomerId(customerId);
        reqVO.setPageSize(PAGE_SIZE_NONE); // 不分页
        return receiptTitleMapper.selectPage(reqVO).getList();
    }
}